// pages/dateselect/dateselect.js
const app = getApp();

var todayT;
var typeD;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    year: 0,

    month: 0,

    date: ['日', '一', '二', '三', '四', '五', '六'],

    isToday: 0,

    isTodayWeek: false,

    todayIndex: 0,

    dateMouths:[],//存储本月和下月的日期，其他的先不管
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.type);

    typeD = options.type;

    let now = new Date();

    let year = now.getFullYear();

    let month = now.getMonth() + 1;

    
    if (typeD == 'ruzhu'){

      todayT = '' + year + month + now.getDate();

    } else if (typeD == 'leave'){
     
      var time1 = app.util.getDateStr(1);
      
      time1 = time1.split('\n')[0];
      var t = time1.split('-')[1];
      var i = t.indexOf("0");
      if(i>=0){
        time1 = time1.split('-0')[0] + time1.split('-0')[1];
      }else{
        time1 = time1.split('-')[0] + time1.split('-')[1];
      }
      
      time1 = year + time1;
      todayT = time1;
    }

    this.setData({

      year: year,

      month: month,

      isToday: todayT

    })
    
    this.dateInit(year, month-1);//当前月

    console.log(year, month,'年月')
    var nextYear,nextMouth;
    if (month >= 12){
      nextYear = year + 1;
      nextMouth = 1;
    }else{
      nextYear = year;
      nextMouth = month ;
    }

    this.dateInit(nextYear, nextMouth);//下月

  },
  dateInit: function (setYear, setMonth) {

    let dateArr = [];						//需要遍历的日历数组数据

    let arrLen = 0;							//dateArr的数组长度

    let now = setYear ? new Date(setYear, setMonth) : new Date();

    let year = setYear || now.getFullYear();

    let nextYear = 0;

    let month = setMonth || now.getMonth();					//没有+1方便后面计算当月总天数

    let nextMonth = (month + 1) > 11 ? 1 : (month + 1);

    let startWeek = new Date(year + ',' + (month + 1) + ',' + 1).getDay();							//目标月1号对应的星期

    let dayNums = new Date(year, nextMonth, 0).getDate();				//获取目标月有多少天

    let obj = {};

    let num = 0;
    if (month + 1 > 11) {

      nextYear = year + 1;

      dayNums = new Date(nextYear, nextMonth, 0).getDate();

    }

    arrLen = startWeek + dayNums;

    for (let i = 0; i < arrLen; i++) {

      if (i >= startWeek) {

        num = i - startWeek + 1;
   
        var weight ;
        if (todayT == '' + year + (month + 1) + num){
          if (typeD == 'ruzhu'){
            weight = '入住时间';

          } else if(typeD == 'leave'){
            weight = '离店时间';
          }
          
        } else {
          weight = '';
        } 

        console.log(weight, typeD, todayT);

        obj = {

          isToday: '' + year + (month + 1) + num,

          dateNum: num,

          weight: weight

        }

      } else {

        obj = {};

      }

      dateArr[i] = obj;

    }
    //
    var ary = this.data.dateMouths;
    ary.push(dateArr);

    this.setData({

      dateMouths: ary

    })

    let nowDate = new Date();

    let nowYear = nowDate.getFullYear();

    let nowMonth = nowDate.getMonth() + 1;

    let nowWeek = nowDate.getDay();

    let getYear = setYear || nowYear;

    let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;

    if (nowYear == getYear && nowMonth == getMonth) {

      this.setData({

        isTodayWeek: true,

        todayIndex: nowWeek

      })

    } else {

      this.setData({

        isTodayWeek: false,

        todayIndex: -1

      })

    }
  },
  dayclick:function(e){
   
    var parent_i = e.currentTarget.dataset.i;
    var i = e.currentTarget.dataset.index;
    todayT = e.currentTarget.dataset.date;

    var ary = this.data.dateMouths;

    var obj = ary[parent_i][i];
    console.log(obj,'obj');

    var weight;
    if (typeD == 'ruzhu') {
      weight = '入住时间';

    } else if (typeD == 'leave') {
      weight = '离店时间';
    }
    obj.weight = weight;

    this.setData({
      isToday: todayT,
      dateMouths: ary
    })

    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面

    var data = obj.isToday;
    data = data.substr(4, data.length - 4);
    data = data.replace(obj.dateNum, '');
   

    data = data + '月' + obj.dateNum;

    if (typeD == 'ruzhu'){
      prevPage.setData({
        ruzhu_time: data
      })
    } else if (typeD == 'leave') {
      prevPage.setData({
        leave_time: data,
        hotel_day:3
      })
    }
   
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})